import React, { Component } from 'react';
import Web3 from 'web3';
import TruffleContract from 'truffle-contract';
import AdoptionJson from '../../woniu-petshop-truffle/build/contracts/Adoptions.json';
import './App.css';

class App extends Component {

  // 1.链接合约
  // 2.执行合约函数
  // 3.添加ant.design ui支持
  // 4.完成项目
    constructor(props) {
        super(props);
        this.web3 = null;
        this.Adoption = null;
        this.init();
    }
    // 初始化web3
    init() {
        if (typeof window.web3 !== 'undefined') {
            this.web3Provider = window.web3.currentProvider;
        } else {
            alert('请安装matemask');
        }
        this.web3 = new Web3(this.web3Provider);
        this.initAdoption();
    }
    //初始化合约
    initAdoption() {
        this.Adoption = TruffleContract(AdoptionJson);
        this.Adoption.setProvider(this.web3Provider);
        return this.markAdopted();
    }
    //读取领养信息
    // await同步方式获取异步数据，方法名字加async 参数 。也可以使用.then方法。参考ES6语法
    async markAdopted() {
        const adoptionInstance = await this.Adoption.deployed();
        const adopters = await  adoptionInstance.getAdopters.call();
        console.log(adopters);
    }
}

export default App;
